package cn.springmvc.controller;

import cn.springmvc.model.Page;
import cn.springmvc.model.User;
import cn.springmvc.service.UserService;
import cn.util.SuccessInfo;
import cn.util.SuccessInfoUtils;
import com.alibaba.fastjson.JSONObject;
import com.meidusa.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
* Created by user on 2015-07-17.
*/

@Controller
@RequestMapping("/")
public class UserController {
    @Autowired
    private UserService userService;
    @ResponseBody
    @RequestMapping("index")
    public Object index(HttpServletRequest request,
                        HttpServletResponse response){
        String pageNow = request.getParameter("pageNow");
        Page page = null;
        List<User> users = null;
        int totalCount = (int)userService.getAllUserCount();
        if (pageNow != null) {
            page = new Page(totalCount, Integer.parseInt(pageNow));
            users = userService.getUsers(page);
        } else {
            page = new Page(totalCount, 1);
            users = userService.getUsers(page);
        }
        page.setDatas(users);
        return SuccessInfoUtils.setSuccessInfo(page);
    }
    @ResponseBody
    @RequestMapping("doLogin")
    public  Object doLogin(HttpServletRequest request){
        String pageNow = request.getParameter("email");
//        System.out.println(JSONObject.toJSONString(params)); @RequestBody Map<String, Object> params,
        System.out.println(pageNow);
        User user = new User();
        user.setNickname("xxxx");
        user.setState(111);
        return SuccessInfoUtils.setSuccessInfo(user);
    }


}
